home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_313 / uucp / uucp1.lzh / src / lib / lockfile.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  2KB  |  110 lines

  1.  
  2. /*
  3.  *  LOCKFILE.C
  4.  *
  5.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  6.  *
  7.  *  Lock and unlock a file.  Under AmigaDOS, openning a file mode 1006
  8.  *  (accomplished with fopen(,"w"), locks the file exclusively.  That
  9.  *  is, further fopen()s will fail.  Thus, we need only keep a live
  10.  *  file descriptor to 'lock' the file.
  11.  *
  12.  *  This is advantagious because if the program exits without removing
  13.  *  the lock file we are still ok... the file is unlocked by virtue of
  14.  *  the file descriptor getting closed.
  15.  */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/lists.h>
  19. #include <proto/all.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. typedef struct List LIST;
  24. typedef struct Node NODE;
  25.  
  26. typedef struct {
  27.     NODE    Node;
  28.     FILE    *Fi;
  29.     short   Refs;
  30. } LNode;
  31.  
  32. LIST LockList = { (NODE *)&LockList.lh_Tail, NULL, (NODE *)&LockList.lh_Head };
  33.  
  34. void FreeLockNode();
  35.  
  36. void
  37. LockFile(file)
  38. char *file;
  39. {
  40.     char *ptr;
  41.  
  42.     LNode *node;
  43.     LNode *n;
  44.  
  45.     for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr);
  46.     ++ptr;
  47.  
  48.  
  49.     if (node = malloc(sizeof(LNode) + strlen(ptr) + 16)) {
  50.     node->Node.ln_Name = (char *)(node + 1);
  51.     sprintf(node->Node.ln_Name, "T:%s.LOCK", ptr);
  52.  
  53.     for (n = (LNode *)LockList.lh_Head; n != (LNode *)&LockList.lh_Tail; n = (LNode *)n->Node.ln_Succ) {
  54.         if (strcmp(node->Node.ln_Name, n->Node.ln_Name) == 0) {
  55.         ++n->Refs;
  56.         free(node);
  57.         return;
  58.         }
  59.     }
  60.  
  61.     while ((node->Fi = fopen(node->Node.ln_Name, "w")) == NULL) {
  62.         sleep(2);
  63.         chkabort();
  64.     }
  65.     node->Refs = 1;
  66.     AddTail(&LockList, &node->Node);
  67.     }
  68. }
  69.  
  70. void
  71. UnLockFile(file)
  72. char *file;
  73. {
  74.     LNode *node;
  75.     short len;
  76.     char *ptr;
  77.  
  78.     for (ptr = file + strlen(file); ptr >= file && *ptr != '/' && *ptr != ':'; --ptr);
  79.     ++ptr;
  80.     len = strlen(ptr);
  81.  
  82.     for (node = (LNode *)LockList.lh_Head; node != (LNode *)&LockList.lh_Tail; node = (LNode *)node->Node.ln_Succ) {
  83.     if (strncmp(ptr, node->Node.ln_Name + 2, len) == 0 && strlen(node->Node.ln_Name) == len + 7) {
  84.         if (--node->Refs == 0)
  85.         FreeLockNode(node);
  86.         return;
  87.     }
  88.     }
  89. }
  90.  
  91. void
  92. UnLockFiles()
  93. {
  94.     LNode *node;
  95.  
  96.     while ((node = (LNode *)LockList.lh_Head) != (LNode *)&LockList.lh_Tail)
  97.     FreeLockNode(node);
  98. }
  99.  
  100. void
  101. FreeLockNode(node)
  102. LNode *node;
  103. {
  104.     Remove(node);
  105.     fclose(node->Fi);
  106.     unlink(node->Node.ln_Name);
  107.     free(node);
  108. }
  109.  
  110.